#!/usr/bin/env bash
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
# This sub Makefile compiles libraries under this directory. This is designed to
# be used as a sub Makefile with tensorflow/contrib/makefile/Makefile.
# You can build targets in this file by including this sub makefile like:
# $ make -f tensorflow/contrib/makefile/Makefile TARGET=<target> \
# SUB_MAKEFILES=\
# $(pwd)/tensorflow/contrib/makefile/sub_makefiles/hexagon_graph_execution/Makefile.in \
# (optional: NDK_ROOT=<ndk_root>) hexagon_graph_execution
# TODO(satok): Support more targets

GTEST_DIR := \
$(MAKEFILE_DIR)/downloads/googletest/googletest

GTEST_HEADERS = \
$(wildcard $(GTEST_DIR)/include/gtest/*.h) \
$(wildcard $(GTEST_DIR)/include/gtest/internal/*.h)

GTEST_SRCS := \
$(wildcard $(GTEST_DIR)/src/*.cc) \
$(wildcard $(GTEST_DIR)/src/*.h) \
$(GTEST_HEADERS)

GRAPH_TRANSFER_SRCS := \
tensorflow/core/platform/posix/test.cc

GRAPH_EXECUTION_SRCS := \
$(GRAPH_TRANSFER_SRCS) \
tensorflow/core/kernels/hexagon/hexagon_graph_execution_test.cc \
tensorflow/contrib/makefile/test/test_main.cc

GRAPH_EXECUTION_OBJS := $(addprefix $(OBJDIR), $(GRAPH_EXECUTION_SRCS:.cc=.o))

GRAPH_EXECUTION_NAME := hexagon_graph_execution
GRAPH_EXECUTION_BIN_PATH := $(BINDIR)$(GRAPH_EXECUTION_NAME)

INCLUDES += \
-I$(MAKEFILE_DIR)/downloads/googletest/googletest/include

GRAPH_EXECUTION_INCLUDES := $(INCLUDES)

$(OBJDIR)gtest-all.o: $(GTEST_SRCS) $(GRAPH_EXECUTION_OBJS)
	$(CXX) $(CXXFLAGS) $(GRAPH_EXECUTION_INCLUDES) -I $(GTEST_DIR) -c \
	$(GTEST_DIR)/src/gtest-all.cc -o $@

$(LIBDIR)gtest.a: $(OBJDIR)gtest-all.o
	@mkdir -p $(dir $@)
	$(AR) $(ARFLAGS) $@ $^

$(GRAPH_EXECUTION_BIN_PATH): $(LIB_PATH) $(LIBDIR)gtest.a $(GRAPH_EXECUTION_OBJS)
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $(GRAPH_EXECUTION_INCLUDES) \
		-o $(GRAPH_EXECUTION_BIN_PATH) $(GRAPH_EXECUTION_OBJS) \
		$(LIBFLAGS) $(LIB_PATH) $(LIBDIR)gtest.a $(LDFLAGS) $(LIBS)

$(GRAPH_EXECUTION_NAME): $(GRAPH_EXECUTION_BIN_PATH)
